﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Reflection;
using System.Data.Linq.Mapping;

namespace BulletOnRails4NET.Controllers
{
    public class GridHelper
    {
        public string column_settings { get; set; }
        public string column_headings { get; set; }
        public int grid_rows_per_page { get; set; }
        public string grid_title { get; set; }

        //To be kept
        public bool clear_fields_on_new { get; set; }
        public string grid_metadata_url { get; set; }
        public string grid_data_url { get; set; }
        public string assoc_data_url { get; set; }
        public string controller_name { get; set; }
        public List<GridColumnModel> column_models { get; set; }
        public List<string> column_names { get; set; }
        public List<GridColumnModel> form_field_models { get; set; }

        public JsonResult grid_meta_data { get; set; }


        public GridHelper(
            string controller_name, PropertyInfo[] grid_columns_info, string grid_data_url,
            int grid_rows_per_page, string grid_title, string grid_metadata_url)
        {
            PopulateSettingsForGrid(controller_name,grid_columns_info, grid_data_url,
             grid_rows_per_page, grid_title, grid_metadata_url);
        }

        //Returns a string containing the javascript necesary to set up the
        // the grid coluns from the Properties passed to this function
        public void PopulateSettingsForGrid(
            string controller_name,
            PropertyInfo[] grid_columns_info, 
            string grid_data_url,
            int grid_rows_per_page, string grid_title, string grid_metadata_url)
        {
            this.grid_data_url = grid_data_url;
             this.grid_rows_per_page = grid_rows_per_page;
             this.grid_title = grid_title;
             this.grid_metadata_url = grid_metadata_url;
             this.controller_name = controller_name;
             assoc_data_url = PathResolver.GetSiteRoot() + controller_name
            + "/GetAssociationsPossibleValues";

            ColumnAttribute ca;
            String field_name, humanized_name;
            object[] info;

            column_models = new List<GridColumnModel>();
            column_names = new List<string>();
            
            foreach (PropertyInfo prop in grid_columns_info)
            {

                //Get the Linq realted info
                info = prop.GetCustomAttributes(typeof(ColumnAttribute), true);
                // Check if it is a Linq column property
                if (info.Length == 1)
                {
                    ca = (ColumnAttribute)info[0];
                    field_name = prop.Name;
                    
                    humanized_name = field_name.HumanizeString();
                    column_names.Add(humanized_name);
                    if (prop.PropertyType == typeof(string))
                    {
                        column_models.Add(new GridColumnModel(humanized_name, 90, field_name));
                    }
                    else
                    {
                             column_models.Add(new GridColumnModel(humanized_name, (humanized_name.Length * 7) + 7, field_name));
                   
                    }
                }
            }
            form_field_models = new List<GridColumnModel>(column_models);
            UpdateGridMetaData();
            clear_fields_on_new = true;

        }

        public void UpdateGridMetaData()
        {
            grid_meta_data = new JsonResult
            {
                Data = new
                {
                    colModel = column_models,
                    colNames = column_names,
                    editFieldsModel = form_field_models
                }
            };
        }

    }
}
